//
//  XMLInterface.m
//  Rainbow
//
//  Created by Guillaume Dupuis on 21/07/09.
//  Copyright 2009 Erasmus Software. All rights reserved.
//

#import "XMLInterface.h"
#import "XMLFile.h"

@implementation XMLInterface

-(id)initPars
{	
	self = [super init];
	if (self != nil)
	{
		_path = [[NSHomeDirectory() stringByAppendingPathComponent: @"Library/Application Support/Rainbow/DvdList.xml"] retain];
		_xmlDoc = [[NSXMLDocument createXMLDocumentFromFile: _path] retain];
	}
	//NSLog(_path);
	//NSLog([_path stringByExpandingTildeInPath]);
	
	return self;
}

-(void)save
{
	BOOL fileWritten=[_xmlDoc writeToFile:_path ofType:nil];
	
	if (fileWritten) 
		NSLog(@"Database saved"); 
	else 
		NSLog(@"Problem in saving the Database");	
}

-(void)listNameOfDvd
{
	NSArray* itemNodes;
	NSError* error;
	int i;
	
	itemNodes = [[_xmlDoc nodesForXPath:@"./dvdtech/dvd" error: &error] retain];
	i = [itemNodes count]; 
	
	while (i > 0)
	{
		NSXMLElement *dvd = [[itemNodes objectAtIndex:(i - 1)] retain];
		NSLog(@"J'ai un Dvd qui s'appelle: %@", [[[dvd elementsForName:kTitle] objectAtIndex:0] stringValue]);
		//		NSLog(@"J'ai un Dvd qui s'appelle: %@", [[dvd attributeForName:kTitle] stringValue]);
		--i;
		[dvd release];
	}
	[itemNodes release];
}

-(void)			loadDvd:(NSMutableArray*)dvds
{
	NSArray* itemNodes;
	NSError* error;
	int i;
	
	itemNodes = [[_xmlDoc nodesForXPath:@"./dvdtech/dvd" error: &error] retain];
	i = [itemNodes count]; 
	
	while (i > 0)
	{
		NSXMLElement *dvd = [[itemNodes objectAtIndex:(i - 1)] retain];
		NSLog(@"Dvd loaded: %@", [[[dvd elementsForName:kTitle] objectAtIndex:0] stringValue]);	
		
		// put the dvd loaded in a Dictionary
		NSDictionary	*dvd_save = [NSDictionary alloc];
		NSString *idmovie;
		NSString *title;
		NSString *director;
		NSString *language;
		NSString *year;
		NSString *actor;
		NSString *type;
		NSString *summary;
		NSString *comment;
		NSString *tags;
		NSString *path;
		

		idmovie = [[dvd attributeForName:kId] stringValue];

		if ([[dvd elementsForName:kTitle] count])
			title = [[[dvd elementsForName:kTitle] objectAtIndex:0] stringValue];
		else
			title = @"";
		if ([[dvd elementsForName:kDirector] count])
			 director = [[[dvd elementsForName:kDirector] objectAtIndex:0] stringValue];
		else
			 director = @"";
		if ([[dvd elementsForName:kLanguage] count])
			 language = [[[dvd elementsForName:kLanguage] objectAtIndex:0] stringValue];
		else
			 language = @"";
		if ([[dvd elementsForName:kYear] count])
			year = [[[dvd elementsForName:kYear] objectAtIndex:0] stringValue];
		else
			year = @"";
		if ([[dvd elementsForName:kActor] count])
			 actor = [[[dvd elementsForName:kActor] objectAtIndex:0] stringValue];
		else
			 actor = @"";
		if ([[dvd elementsForName:kType] count])
			 type = [[[dvd elementsForName:kType] objectAtIndex:0] stringValue];
		else
			 type = @"";
		if ([[dvd elementsForName:kSummary] count])
			 summary = [[[dvd elementsForName:kSummary] objectAtIndex:0] stringValue];
		else
			 summary = @"";
		if ([[dvd elementsForName:kComment] count])
			comment = [[[dvd elementsForName:kComment] objectAtIndex:0] stringValue];
		else
			comment = @"";
		if ([[dvd elementsForName:kTags] count])
			tags = [[[dvd elementsForName:kTags] objectAtIndex:0] stringValue];
		else
			tags = @"";
		if ([[dvd elementsForName:kPath] count])
			path = [[[dvd elementsForName:kPath] objectAtIndex:0] stringValue];
		else
			path = @"";
	
		dvd_save = [NSDictionary	dictionaryWithObjectsAndKeys:idmovie, kId,
					title, kTitle,
					director, kDirector,
					language, kLanguage,
					year, kYear,
					type, kType,
					actor, kActor,
					summary, kSummary,
					comment, kComment,
					tags, kTags,
					path, kPath, nil];
		
		[dvds addObject:dvd_save];

		--i;
		[dvd release];
	}
	[itemNodes release];
}

-(NSInteger)	getNewId
{
	
	NSInteger	new_id;
	NSArray		*itemNodes;
	NSError		*error;
	
	itemNodes = [[_xmlDoc nodesForXPath:@"./dvdtech/dvd" error: &error] retain];
	if ([itemNodes count])
		new_id = [[[[itemNodes objectAtIndex:([itemNodes count] - 1)] attributeForName:kId] stringValue] integerValue] + 1;
	else
		new_id = 0;
	[itemNodes release];
	
	return (new_id);
}

-(void)addDvd:(NSDictionary *)dvd
{
	NSError			*error;
	NSXMLElement	*dvdtech=[[[_xmlDoc nodesForXPath:@"./dvdtech" error:&error] lastObject] retain];
	
	if (!dvdtech)
	{
		return;
	}
	
		
	NSString *movie = [[NSString alloc] initWithFormat:@"<dvd Id=\"%@\"><Title>%@</Title>\
					   <Director>%@</Director><Language>%@</Language><Year>%@</Year><Type>%@</Type>\
					   <Actor>%@</Actor><Summary>%@</Summary><Comment>%@</Comment><Tags>%@</Tags>\
					   <Path>%@</Path></dvd>", [dvd valueForKey:kId], [dvd valueForKey:kTitle], [dvd valueForKey:kDirector], [dvd valueForKey:kLanguage],
					   [dvd valueForKey:kYear], [dvd valueForKey:kType], [dvd valueForKey:kActor],
					   [dvd valueForKey:kSummary], [dvd valueForKey:kComment], [dvd valueForKey:kTags], [dvd valueForKey:kPath]];
	

	NSXMLElement *xml_dvd = [[[NSXMLElement alloc] initWithXMLString:movie error:&error] retain];
	if (!xml_dvd)
		NSLog(@"erreur xml adding: %@", error);
	
	[dvdtech addChild:xml_dvd];
	
	[dvdtech release];
	[xml_dvd release];
}

-(NSXMLElement*)searchID:(NSInteger)id
{
	NSArray			*itemNodes = NULL;
	NSError			*error = NULL;
	int				i = 0;
	NSXMLElement	*dvd = NULL;
	
	itemNodes = [[_xmlDoc nodesForXPath:@"./dvdtech/dvd" error: &error] retain];
	
	while (i < [itemNodes count])
	{
		dvd = [itemNodes objectAtIndex:i];
		if (id == [[[dvd attributeForName:kId] stringValue] integerValue])
			break;
		++i;
	}
	
	if (i == [itemNodes count])	
		return NULL;		
	else
		return dvd;
}

-(void)deleteDvd:(NSInteger)id
{
	NSXMLElement *dvd = NULL;
	
	dvd = [self searchID:id];
	
	[dvd detach];
}

-(void)	setDvdOwner:(NSString *)owner
{
	NSArray			*itemNodes = NULL;
	NSError			*error = NULL;

	itemNodes = [[_xmlDoc nodesForXPath:@"./dvdtech" error: &error] retain];	
	NSXMLNode *own = [[itemNodes objectAtIndex:0] attributeForName:@"owner"];
	
	[own setStringValue: owner];

	[itemNodes release];
}


- (NSString *)windowNibName
{
	// Implement this to return a nib to load OR implement -makeWindowControllers to manually create your controllers.
	return @"Parsing";
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
	// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
	
	// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
	
	// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
	
	return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
	// Insert code here to read your document from the given data of the specified type.  If the given outError != NULL, ensure that you set *outError when returning NO.
	
	// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. 
	
	// For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
	
	return YES;
}

@end
